/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package idea.sfo.viewer.graphics;


import designersfo.Cons.CellInfo.CellFormat;
import idea.model.qnrlibrary.CustomResponses;
import idea.model.qnrlibrary.DcAttributeGroup;
import idea.model.qnrlibrary.PaperAttributeGroup;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;


/**
 *
 * @author renke002
 */
public class AnswerField implements Drawable
{

    /**
     *
     */
    public static final float LINE_THICKNESS_INCH = (float) .007;
    /**
     *
     */
    public static final float FIELD_HEIGHT_INCH = (float) .311;
    /**
     *
     */
    public static final float SEGMENT_BOX_WIDTH_INCH = (float) .200;
    /**
     *
     */
    public static final float SEGMENT_HEIGHT_INCH = (float) .063;
    /**
     *
     */
    private CellBorder border;
    /**
     *
     */
    private Rectangle container;
    /**
     *
     */
    private double fudgeFactor = 94;
    /**
     *
     */
    private ArrayList<Rectangle> innerSegments;


    /**
     *
     * @param cResponses
     * @param dcAttrGroup
     * @param paperAttrGroup
     * @param container
     * @param cellFormat
     */
    AnswerField(
            CustomResponses cResponses ,
            DcAttributeGroup dcAttrGroup ,
            PaperAttributeGroup paperAttrGroup ,
            Rectangle container ,
            CellFormat cellFormat )
    {
        //
        this.container = container;

        //
        short segmentsNum = paperAttrGroup.getNumSegments();
        String tickPattern = paperAttrGroup.getTickPattern();

        //
        Rectangle answerBox = this.calcAnswerBoxSize(
                container ,
                segmentsNum ,
                tickPattern );

        //
        this.innerSegments = this.setupSegments(
                answerBox ,
                segmentsNum ,
                tickPattern );

        // plain border
        this.border = new CellBorder( answerBox );

    }


    /**
     *
     * @param container
     * @param segmentsNum
     * @param tickPattern
     * @return
     */
    private Rectangle calcAnswerBoxSize( Rectangle container , short segmentsNum , String tickPattern )
    {
        int height = (int) ( AnswerField.FIELD_HEIGHT_INCH * this.fudgeFactor );
        int width = (int) ( ( AnswerField.SEGMENT_BOX_WIDTH_INCH * segmentsNum ) * this.fudgeFactor );

        Dimension answerBoxSize = new Dimension( width , height );

        Point anchorPoint = ContentLayout.placeContent( container ,
                answerBoxSize , ContentLayout.HORIZONTAL_NAME_CENTER ,
                ContentLayout.VERTICAL_NAME_CENTER );

        Rectangle answerBox = new Rectangle( anchorPoint , answerBoxSize );
        return answerBox;
    }


    /**
     *
     * @param g2d
     */
    @Override
    public void draw( Graphics2D g2d )
    {
        // draw the white background.
        g2d.setColor( Color.WHITE );
        g2d.fill( container );

        // draw the outer border.
        this.border.draw( g2d );

        // draw the inner components.
        g2d.setColor( Color.GRAY );
        for ( Rectangle segment : this.innerSegments )
        {
            g2d.draw( segment );
        }

        // what else to draw?
    }


    /**
     *
     * @param answerBox
     * @param segmentsNum
     * @param tickPattern
     * @return
     */
    private ArrayList<Rectangle> setupSegments( Rectangle answerBox , short segmentsNum , String tickPattern )
    {
        ArrayList<Rectangle> segments = new ArrayList<>();

        // starting point for the x-coordinate
        int segmentPosX = answerBox.x + (int) ( AnswerField.LINE_THICKNESS_INCH * this.fudgeFactor );

        //
        int segmentTopY = answerBox.y + 1;
        // 
        int segmentBaseY = answerBox.y - 1 + (int) ( ( AnswerField.FIELD_HEIGHT_INCH - AnswerField.SEGMENT_HEIGHT_INCH ) * this.fudgeFactor );

        //
        int segmentHeight = (int) ( AnswerField.SEGMENT_HEIGHT_INCH * this.fudgeFactor );
        //
        int segmentWidth = 0;

        Rectangle segment;
        for ( short i = 1; i < segmentsNum; i++ )
        {
            // shift the x-coordinate by the size of a box.
            segmentPosX += (int) ( AnswerField.SEGMENT_BOX_WIDTH_INCH * this.fudgeFactor );

            // LOWER SEGMENT
            segment = new Rectangle(
                    segmentPosX , segmentBaseY ,
                    segmentWidth , segmentHeight );
            segments.add( segment );

            // UPPER SEGMENT
            segment = new Rectangle(
                    segmentPosX , segmentTopY ,
                    segmentWidth , segmentHeight );
            segments.add( segment );
        }

        return segments;
    }

}
